package org.jeecg.modules.htxincailiao.account.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.stream.Collectors;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.util.UUIDGenerator;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.htxincailiao.account.constant.HTAccountTypeEnum;
import org.jeecg.modules.htxincailiao.account.entity.HTGuest;
import org.jeecg.modules.htxincailiao.account.service.IHTGuestService;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;

import org.jeecg.modules.htxincailiao.im.entity.IMUser;
import org.jeecg.modules.htxincailiao.im.service.IIMUserService;
import org.jeecg.modules.vendors.tencent.official_account.config.Account;
import org.jeecgframework.poi.excel.ExcelImportUtil;
import org.jeecgframework.poi.excel.def.NormalExcelConstants;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.entity.ImportParams;
import org.jeecgframework.poi.excel.view.JeecgEntityExcelView;
import org.jeecg.common.system.base.controller.JeecgController;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;
import com.alibaba.fastjson.JSON;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.jeecg.common.aspect.annotation.AutoLog;

/**
 * @Description: ht_guest
 * @Author: jeecg-boot
 * @Date: 2023-03-12 16:35:18
 * @Version: V1.0
 */
@Api(tags = "ht_guest")
@RestController
@RequestMapping("/account/hTGuest")
@Slf4j
public class HTGuestController extends JeecgController<HTGuest, IHTGuestService> {
    @Autowired
    private IHTGuestService hTGuestService;

    @Resource
    private IIMUserService imUserService;


    /**
     * 添加访客
     *
     * @param hTGuest
     * @return
     */
    @AutoLog(value = "ht_guest-添加访客")
    @ApiOperation(value = "ht_guest-添加访客", notes = "ht_guest-添加访客")
    @PostMapping(value = "/getGuest")
    public Result<?> getGuest(@RequestBody HTGuest hTGuest) {
        if (oConvertUtils.isEmpty(hTGuest.getId())) {
            return Result.error("访客指纹信息不能为空");
        }

        HTGuest storedGuest = hTGuestService.getById(hTGuest.getId());
        if (oConvertUtils.isNotEmpty(storedGuest)) {
            IMUser imUser = imUserService.queryByUserId(storedGuest.getId());
            if(oConvertUtils.isEmpty(imUser)) {
                updateGuest(storedGuest);
                imUser = copyIMUser(storedGuest);
                imUserService.save(imUser);
            }
            return Result.OK(storedGuest);

        } else {
            updateGuest(hTGuest);
            hTGuestService.save(hTGuest);

            IMUser imUser = copyIMUser(hTGuest);
            imUserService.save(imUser);

            return Result.OK(hTGuest);
        }
    }


    private void updateGuest(HTGuest hTGuest) {
        int length = 8;
        StringBuilder sb = new StringBuilder();
        Random random = new Random();
        for(int i = 0; i < length; i++) {
            sb.append(random.nextInt(10));
        }
        String randomNum = sb.toString();
        hTGuest.setNickname("访客-"+randomNum);
        hTGuest.setUsername(hTGuest.getId());
        hTGuest.setPassword(hTGuest.getId());
        hTGuest.setPhoneNumber(hTGuest.getId());
    }

    private IMUser copyIMUser(HTGuest hTGuest) {


        IMUser imUser = new IMUser();
        BeanUtils.copyProperties(hTGuest, imUser);
        imUser.setId(null);
        imUser.setUserId(hTGuest.getId());
        imUser.setAccountType(HTAccountTypeEnum.ALLIANCE_GUEST.getValue());

        return imUser;

    }


    /**
     * 分页列表查询
     *
     * @param hTGuest
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    @AutoLog(value = "ht_guest-分页列表查询")
    @ApiOperation(value = "ht_guest-分页列表查询", notes = "ht_guest-分页列表查询")
    @GetMapping(value = "/list")
    public Result<?> queryPageList(HTGuest hTGuest, @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo, @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize, HttpServletRequest req) {
        QueryWrapper<HTGuest> queryWrapper = QueryGenerator.initQueryWrapper(hTGuest, req.getParameterMap());
        Page<HTGuest> page = new Page<HTGuest>(pageNo, pageSize);
        IPage<HTGuest> pageList = hTGuestService.page(page, queryWrapper);
        return Result.OK(pageList);
    }

    /**
     * 添加
     *
     * @param hTGuest
     * @return
     */
    @AutoLog(value = "ht_guest-添加")
    @ApiOperation(value = "ht_guest-添加", notes = "ht_guest-添加")
    @PostMapping(value = "/add")
    public Result<?> add(@RequestBody HTGuest hTGuest) {
        hTGuestService.save(hTGuest);
        return Result.OK("添加成功！");
    }

    /**
     * 编辑
     *
     * @param hTGuest
     * @return
     */
    @AutoLog(value = "ht_guest-编辑")
    @ApiOperation(value = "ht_guest-编辑", notes = "ht_guest-编辑")
    @PutMapping(value = "/edit")
    public Result<?> edit(@RequestBody HTGuest hTGuest) {
        hTGuestService.updateById(hTGuest);
        return Result.OK("编辑成功!");
    }

    /**
     * 通过id删除
     *
     * @param id
     * @return
     */
    @AutoLog(value = "ht_guest-通过id删除")
    @ApiOperation(value = "ht_guest-通过id删除", notes = "ht_guest-通过id删除")
    @DeleteMapping(value = "/delete")
    public Result<?> delete(@RequestParam(name = "id", required = true) String id) {
        hTGuestService.removeById(id);
        return Result.OK("删除成功!");
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @AutoLog(value = "ht_guest-批量删除")
    @ApiOperation(value = "ht_guest-批量删除", notes = "ht_guest-批量删除")
    @DeleteMapping(value = "/deleteBatch")
    public Result<?> deleteBatch(@RequestParam(name = "ids", required = true) String ids) {
        this.hTGuestService.removeByIds(Arrays.asList(ids.split(",")));
        return Result.OK("批量删除成功!");
    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    @AutoLog(value = "ht_guest-通过id查询")
    @ApiOperation(value = "ht_guest-通过id查询", notes = "ht_guest-通过id查询")
    @GetMapping(value = "/queryById")
    public Result<?> queryById(@RequestParam(name = "id", required = true) String id) {
        HTGuest hTGuest = hTGuestService.getById(id);
        if (hTGuest == null) {
            return Result.error("未找到对应数据");
        }
        return Result.OK(hTGuest);
    }

    /**
     * 导出excel
     *
     * @param request
     * @param hTGuest
     */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, HTGuest hTGuest) {
        return super.exportXls(request, hTGuest, HTGuest.class, "ht_guest");
    }

    /**
     * 通过excel导入数据
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, HTGuest.class);
    }

}
